package com.ctf.soft.ms.controls.common;

import com.ctf.soft.ms.dao.dto.manage.LoginUser;
import com.ctf.soft.ms.dao.dto.manage.PurviewMenu;
import com.ctf.soft.ms.dao.model.manage.User;
import com.ctf.soft.ms.service.manage.IMenuService;
import com.ctf.soft.ms.service.manage.IUserService;
import com.ctf.soft.ms.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MainController extends BaseController {

    @Autowired
    private IMenuService menuService;
    @Autowired
    private IUserService userService;

    @RequestMapping("/main")
    public String doMain(HttpSession session) {
        LoginUser loginUser = loadSession(session);
        if (loginUser != null) {
            System.out.println("user:[" + loginUser.getAccount() + "]-["
                    + loginUser.getRealName() + "]");
            System.out.println("org:[" + loginUser.getDefaultOrg() + "]");
            System.out.println("dept:[" + loginUser.getDefaultDept() + "]");
            System.out.println("post:[" + loginUser.getDefaultPost() + "]");
            System.out.println("group:[" + loginUser.getDefaultGroup() + "]");
            System.out.println("role:[" + loginUser.getDefaultRole() + "]");
            System.out.println("purview:[" + loginUser.getDefaultPurview()
                    + "]");
            System.out.println("menu:[" + loginUser.getDefaultMenu() + "]");
        }
        return "index";
    }

    @RequestMapping("/PurviewMenu")
    @ResponseBody
    public List<PurviewMenu> loadPurviewMenu(HttpServletRequest request,
                                             HttpSession session) {
        List<PurviewMenu> list = null;
        try {
            String mark = request.getParameter("mark") == null ? "" : request.getParameter("mark").toString().trim();
            list = menuService.loadPurviewMenu(mark);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @RequestMapping("/pwd")
    public String doPwd(HttpSession session) {
        return "pwd";
    }

    @RequestMapping("/RandomPwd")
    @ResponseBody
    public String loadRandomPwd() {
        return ToolsUtil.getRandom();
    }

    @RequestMapping("/UpdatePwd")
    @ResponseBody
    public String doUpdatePwd(@RequestBody ModelMap model,
                              HttpServletRequest request, HttpSession session) {
        String result = "faild";
        try {
            String id = model.get("id").toString().trim();
            User user = userService.selectByPrimaryKey(id);
            String newPwd = ToolsUtil.encryptedPwd(model.get("newPwd")
                    .toString().trim());
            user.setPassword(newPwd);
            int c = userService.updateByPrimaryKey(user);
            if (c > 0) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/CheckRight")
    @ResponseBody
    public String doCheckRight(@RequestBody ModelMap model,
                               HttpServletRequest request, HttpSession session) {
        String result = "faild";
        try {
            String id = model.get("id").toString().trim();
            User user = userService.selectByPrimaryKey(id);
            String pwd = ToolsUtil.encryptedPwd(model.get("col").toString()
                    .trim());
            if (!user.getPassword().trim().equals(pwd)) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
